#include "VideoWidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QApplication>
#include <QPainter>
#include <QDebug>

VideoWidget::VideoWidget(const QString &title, QWidget *parent)
    : QWidget(parent)
{
    setupUI();
    setupConnections();
}

VideoWidget::~VideoWidget()
{
}

void VideoWidget::setupUI()
{
    m_videoContainer = new QWidget();
    m_videoContainer->setObjectName("videoContainer");
    m_videoContainer->setStyleSheet(R"(
        QWidget#videoContainer {
            background-color: #021D43;
            border: 1px solid #165EC2;
            border-radius: 4px;
            min-height: 200px;
        }
    )");

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->addWidget(m_videoContainer);
    
    setupOverlayUI();
}

void VideoWidget::setupConnections()
{
}

void VideoWidget::setupOverlayUI()
{
    m_overlayWidget = new QWidget(this);
    m_overlayWidget->setStyleSheet(R"(
        QWidget {
            background: transparent;
            border: none;
        }
    )");

    // 悬浮界面布局
    m_overlayLayout = new QHBoxLayout(m_overlayWidget);
    m_overlayLayout->setContentsMargins(24, 0, 24, 12);
    m_overlayLayout->setSpacing(0);

    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->setContentsMargins(0, 0, 0, 0);
    vLayout->setSpacing(12);

    // 状态布局
    m_statusLayout = new QHBoxLayout();
    m_statusLayout->setSpacing(8);
    m_statusLabel = new QLabel("当前状态");
    m_statusLabel->setStyleSheet(R"(
        QLabel {
            color: #BCD7FF;
            font-size: 20px;
            font-weight: medium;
            background-color: transparent;
        }
    )");
    m_statusValueLabel = new QLabel("预喷");
    m_statusValueLabel->setFixedSize(112, 40);
    m_statusValueLabel->setStyleSheet(R"(
        QLabel {
            color: #FF1717;
            font-size: 24px;
            font-weight: bold;
            background-color: rgba(255, 116, 116, 0.16);
            border: 1px solid #FF2525;
            border-radius: 2px;
        }
    )");
    m_statusValueLabel->setAlignment(Qt::AlignCenter);
    m_statusLayout->addWidget(m_statusLabel);
    m_statusLayout->addWidget(m_statusValueLabel);
    m_statusLayout->addStretch();

    // 建议布局
    QWidget *suggestionWidget = new QWidget();
    suggestionWidget->setFixedHeight(56);
    suggestionWidget->setStyleSheet(R"(
        QWidget {
            background-color: #314C7A;
            border: 1px solid rgba(147, 210, 255, 0.2);
            border-radius: 4px;
        }
    )");
    m_suggestionLayout = new QHBoxLayout(suggestionWidget);
    m_suggestionLayout->setContentsMargins(0, 0, 0, 0);
    m_suggestionLayout->setSpacing(6);
    m_suggestionLabel = new QLabel("异常操作建议:");
    m_suggestionLabel->setStyleSheet(R"(
        QLabel {
            color: #FFA91F;
            font-size: 28px;
            font-weight: bold;
            background-color: transparent;
            border: none;
        }
    )");
    m_suggestionValueLabel = new QLabel("加入石灰300kg,降枪10cm");
    m_suggestionValueLabel->setStyleSheet(R"(
        QLabel {
            color: #2FFAFD;
            font-size: 28px;
            font-weight: medium;
            background-color: transparent;
            border: none;
        }
    )");
    m_suggestionLayout->addWidget(m_suggestionLabel);
    m_suggestionLayout->addWidget(m_suggestionValueLabel);
    m_suggestionLayout->addStretch();

    vLayout->addLayout(m_statusLayout);
    vLayout->addWidget(suggestionWidget);

    // 按钮布局
    m_buttonLayout = new QHBoxLayout();
    m_buttonLayout->setContentsMargins(0, 0, 0, 0);
    m_buttonLayout->setSpacing(12);
    
    // 正常按钮
    m_normalWt = new QLabel("正常");
    m_normalWt->setStyleSheet(R"(
        QLabel {
            background-image: url(:/img/images/normal.png);
            background-repeat: no-repeat;
            background-position: center;
            color: #FFFFFF;
            border: none;
            border-radius: 50px;
            padding: 12px 24px;
            min-width: 80px;
            min-height: 80px;
            font-size: 16px;
            font-weight: bold;
        }
    )");
    m_normalWt->setAlignment(Qt::AlignCenter);
    m_normalWt->setScaledContents(true);
    m_normalWt->show(); // 确保默认可见
    
    // 异常按钮
    m_abnormalWt = new QLabel("异常");
    m_abnormalWt->setStyleSheet(R"(
        QLabel {
            background-image: url(:/img/images/abnormal.png);
            background-repeat: no-repeat;
            background-position: center;
            color: #FFFFFF;
            border: none;
            border-radius: 50px;
            padding: 12px 24px;
            min-width: 80px;
            min-height: 80px;
            font-size: 16px;
            font-weight: bold;
        }
    )");
    m_abnormalWt->setAlignment(Qt::AlignCenter);
    m_abnormalWt->setScaledContents(true);
    m_abnormalWt->show(); // 确保默认可见
    
    m_buttonLayout->addWidget(m_normalWt);
    m_buttonLayout->addWidget(m_abnormalWt);

    // 添加到主布局
    m_overlayLayout->addLayout(vLayout);
    m_overlayLayout->addStretch();
    m_overlayLayout->addLayout(m_buttonLayout);
    
    // 设置悬浮界面位置和大小
    m_overlayWidget->setGeometry(0, height() / 4 * 3, width(), height() / 4);
    m_overlayWidget->raise(); // 确保在最上层
}

WId VideoWidget::getVideoWindowHandle() const
{
    if (m_videoContainer) {
        return m_videoContainer->winId();
    }
    return 0;
}

void VideoWidget::setCurrentStatus(const QString &status)
{
    if (m_statusValueLabel) {
        m_statusValueLabel->setText(status);
    }
}

void VideoWidget::setOperationSuggestion(const QString &suggestion)
{
    if (m_suggestionValueLabel) {
        m_suggestionValueLabel->setText(suggestion);
    }
}

void VideoWidget::setAbnormal(bool abnormal)
{
    if (m_normalWt && m_abnormalWt) {
        if (abnormal) {
            m_normalWt->setStyleSheet(R"(
                QLabel {
                    background-image: url(:/img/images/normal.png);
                    background-repeat: no-repeat;
                    background-position: center;
                    color: rgba(255, 255, 255, 0.6);
                    border: none;
                    border-radius: 50px;
                    padding: 12px 24px;
                    min-width: 80px;
                    min-height: 80px;
                    font-size: 16px;
                    font-weight: bold;
                }
            )");
            m_abnormalWt->setStyleSheet(R"(
                QLabel {
                    background-image: url(:/img/images/abnormal.png);
                    background-repeat: no-repeat;
                    background-position: center;
                    color: #FFFFFF;
                    border: none;
                    border-radius: 50px;
                    padding: 12px 24px;
                    min-width: 80px;
                    min-height: 80px;
                    font-size: 16px;
                    font-weight: bold;
                }
            )");
        } else {
            m_normalWt->setStyleSheet(R"(
                QLabel {
                    background-image: url(:/img/images/normal.png);
                    background-repeat: no-repeat;
                    background-position: center;
                    color: #FFFFFF;
                    border: none;
                    border-radius: 50px;
                    padding: 12px 24px;
                    min-width: 80px;
                    min-height: 80px;
                    font-size: 16px;
                    font-weight: bold;
                }
            )");
            m_abnormalWt->setStyleSheet(R"(
                QLabel {
                    background-image: url(:/img/images/abnormal.png);
                    background-repeat: no-repeat;
                    background-position: center;
                    color: rgba(255, 255, 255, 0.6);
                    border: none;
                    border-radius: 50px;
                    padding: 12px 24px;
                    min-width: 80px;
                    min-height: 80px;
                    font-size: 16px;
                    font-weight: bold;
                }
            )");
        }
    }
}

void VideoWidget::setButtonDisplayMode(ButtonDisplayMode mode)
{
    if (!m_normalWt || !m_abnormalWt || !m_buttonLayout) {
        return;
    }

    bool normalVisible = false;
    bool abnormalVisible = false;

    switch (mode) {
        case ShowBothButtons:
            normalVisible = true;
            abnormalVisible = true;
            break;
        case ShowNormalButtonOnly:
            normalVisible = true;
            abnormalVisible = false;
            break;
        case ShowAbnormalButtonOnly:
            normalVisible = false;
            abnormalVisible = true;
            break;
    }
    
    // 设置按钮可见性
    m_normalWt->setVisible(normalVisible);
    m_abnormalWt->setVisible(abnormalVisible);
    
    // 强制更新布局
    m_buttonLayout->invalidate();
    m_buttonLayout->activate();
    
    // 更新父组件
    if (m_overlayWidget) {
        m_overlayWidget->updateGeometry();
        m_overlayWidget->update();
    }
    update();
}

void VideoWidget::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    
    // 调整悬浮界面大小
    if (m_overlayWidget) {
        m_overlayWidget->setGeometry(0, height() / 4 * 3, width(), height() / 4);
        m_overlayWidget->raise(); // 确保始终在最上层
    }
}
